#import absl
import logging
logging.basicConfig(level=logging.DEBUG)
import ray
import calc_manager
import pyNastran

from calc_manager.base.runnable import Runnable, runnable_task
from calc_manager.executor.exec import *
from calc_manager.operator import Operator, OperatorInfo, print_operator_info
from calc_manager.ray.ray_manager import RayManager
from calc_manager.ray.remote_task import RayRemoteRunnableTaskFatory
from calc_manager.task.localtask import DefaultTaskFactory



local_operator_info_args = {
    "operator_name": "test_refine_demo",
    "operator_path": "operators_sample/sample_refine_allinone/ref.py",
    "operator_func_entrypoint": "refine_main",
    "operator_desc": "This is a test refine operator",
}
local_operator_info = OperatorInfo(**local_operator_info_args)
remote_local_info_args = {
    "operator_name": "test_refine_demo",
    "operator_path": "sample_refine_allinone/ref.py",
    "operator_func_entrypoint": "refine_main",
    "operator_desc": "This is a test refine operator",
}
remote_operator_info = OperatorInfo(**remote_local_info_args)


from calc_manager.base.plugins import PluginInfo
from calc_manager.plugins.plugins_manager import PluginManager
from calc_manager.base.fields import FieldInfo, REQUIRED_KEY, OPTIONAL_KEY

plugin_bdf_reader = PluginInfo(
    plugin_name="bdf_reader_inner",
    plugin_class_name='BDFReader',
    plugin_module_name='calc_manager.plugins_inner.bdf_reader_inner',
    plugin_class_param_name='bdf_reader',
    plugin_description='bdf reader',
    plugin_fields={
        "field1": FieldInfo(name="oss_url", type="str", optional_field=REQUIRED_KEY, default_val=None, description="osspath"),
        "field2": FieldInfo(name="local_path", type="str", optional_field=OPTIONAL_KEY, default_val=None, description="osspath"),
    }
)
plugin_op2_reader = PluginInfo(
    plugin_name="op2_reader_inner",
    plugin_class_name='OP2Reader',
    plugin_module_name='calc_manager.plugins_inner.op2_reader_inner',
    plugin_class_param_name='op2_reader',
    plugin_description='op2 reader',
    plugin_fields={
        "field1": FieldInfo(name="oss_url", type="str", optional_field=REQUIRED_KEY, default_val=None, description="osspath"),
        "field2": FieldInfo(name="local_path", type="str", optional_field=OPTIONAL_KEY, default_val=None, description="osspath"),
    }
)
plugin_mat_csv_reader = PluginInfo(
    plugin_name="mat_csv_inner",
    plugin_class_name='MatCSVReader',
    plugin_module_name='calc_manager.plugins_inner.mat_csv_inner',
    plugin_class_param_name='mat_csv_reader',
    plugin_description='mat csv reader',
    plugin_fields={
        "field1": FieldInfo(name="oss_url", type="str", optional_field=REQUIRED_KEY, default_val=None, description="osspath"),
        "field2": FieldInfo(name="local_path", type="str", optional_field=OPTIONAL_KEY, default_val=None, description="osspath"),
    }
)

plugin_bdf_transformer = PluginInfo(
    plugin_name="bdf_transformer_inner",
    plugin_class_name='BDFTransformer',
    plugin_module_name='calc_manager.plugins_inner.bdf_transformer_inner',
    plugin_class_param_name='bdf_transformer',
    plugin_description='bdf transforeme ',
    plugin_fields={
        "field1": FieldInfo(name="oss_url", type="str", optional_field=REQUIRED_KEY, default_val=None, description="osspath"),
        "field2": FieldInfo(name="local_path", type="str", optional_field=OPTIONAL_KEY, default_val=None, description="osspath"),
    }
)

pm = PluginManager()
pm.register_plugin(plugin_bdf_reader)
pm.register_plugin(plugin_op2_reader)
pm.register_plugin(plugin_mat_csv_reader)
pm.register_plugin(plugin_bdf_transformer)
plugins_argv={
    "bdf_reader_inner": 
    {
        "oss_url": "https://oss.de3.cc:62135/public/panel.bdf",
        "local_path": "/root/tmp/model/C919_S20_D20200422.bdf"
    },
    "op2_reader_inner": 
    {
        "oss_url": "https://oss.de3.cc:62135/public/panel.op2",
        "local_path": "/root/tmp/model/C919_stress_250206_2DV2W0N94.op2"
    },
    "mat_csv_inner": 
    {
        "oss_url": "https://oss.de3.cc:62135/public/fcy.csv",
    },
    "bdf_transformer_inner":
    {
        "oss_url": "https://oss.de3.cc:62135/public/ss.bdf",
        "local_path": "/root/tmp/model/S20.bdf"
    },
}
plugins_argv_test={
}

default_exec = DefaultExecutor()
local_task_result = exec_operator_task(
    default_exec,
    local_operator_info,
    DefaultTaskFactory(pm, plugins_argv),
    param={"sample_num": 4, "sample_float": 4.0},
)

#manager = RayManager(
#    "ray://10.130.9.3:10001",
#    {
#        "runtime_env": {
#            "py_modules": [calc_manager],
#            "working_dir": "./operators_sample",
#            "pip": ["pyNastran"],
#            "setup_timeout_seconds": 1000,
#        }
#    },
#)
#
#remote_task_result = exec_operator_task(
#    manager,
#    remote_operator_info,
#    RayRemoteRunnableTaskFatory(pm, plugins_argv_test),
#    param={"sample_num": 4, "sample_float": 4.0},
#)
#
##print(f"local task result {local_task_result}")
#print(f"remote task result {remote_task_result}")